package com.deyuanyun.pic.web.utils;

import com.deyuanyun.pic.cache.SessionCacheSupport2;
import com.deyuanyun.pic.common.util.ConvertUtil;
import com.deyuanyun.pic.common.util.web.RequestResponseContext;
import com.deyuanyun.pic.controller.dto.UserSessionVO;
import com.deyuanyun.pic.domain.prvlg.CustomUser;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @ClassName: SecurityUtils
 *
 * @Description: 安全框架处理类
 * @author huanglw
 * @date 2015-8-1 上午10:58:22
 * @updater
 * @version V1.0.0
 *
 * @remarks:
 */
public class SecurityUtils {

    //public static User user = null;

    public static void setUser(CustomUser u){
        HttpServletRequest request = RequestResponseContext.getRequest();
        request.getSession().setAttribute(SessionCacheSupport2.USERLOGINID, u.getUsername());
        UserSessionVO sessionVO=new UserSessionVO();
        ConvertUtil.coverWithSpring(u,sessionVO);
        sessionVO.setMoblie(u.getMoblie());
        sessionVO.setSessionID(request.getSession().getId());
        sessionVO.setLastLoginTime(new Date());

        //sessionVO.setPwd("");
        SessionCacheSupport2.put(sessionVO);
        //todo 此处可以增加登陆日志
    }

    /**
     * 得到当前登录用户
     *
     * @return com.deyuanyun.pic.domain.settings.prvlg.User 返回当前登录用户
     */
    /*public static User currentUser(){
        return user == null ? new User() : user;
    }*/

    public static UserSessionVO currentUserSessionVO(){
        return SessionCacheSupport2.getSessionVO();
    }

    public static void removeUser(HttpServletRequest request){
        UserSessionVO sessionVO= SessionCacheSupport2.getSessionVO();
        if (sessionVO==null){
            return;
        }
        SessionCacheSupport2.remove(sessionVO.getUsername());
        HttpSession session = request.getSession();
        session.invalidate();
    }


//    public static User currentUser() {
//        try {
//            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//
//            return authentication == null ? null : (User) authentication.getPrincipal();
//        } catch (Exception e) {
//            throw new PmcBussnissException(ErrorCode.CUSTOM_ERROR, "没有找到登录用户");
//        }
//    }
//

    /**
     * 密码加密工具
     * @param userName
     * @param pwd
     * @return
     */
    public static String encodePassword(String userName, String pwd) {
        String salt="_gas_pipe_";//加盐
        ShaPasswordEncoder messageDigestPasswordEncoder = new ShaPasswordEncoder(256);
        messageDigestPasswordEncoder.setEncodeHashAsBase64(true);
        return messageDigestPasswordEncoder.encodePassword(userName+pwd, salt);
    }
//
//    public static Member currentUserInPersist() {
//        Member user = currentUser();
//        return Member.read(Member.class, user.getId());
//    }
//
//    public static Member currentUserInPersistSession() {
//        try {
//            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//            return authentication == null ? null : (Member) authentication.getPrincipal();
//        } catch (Exception e) {
//            return null;
//        }
//    }
}
